package com.administrator.customviewtest.successview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.administrator.customviewtest.R;
import com.administrator.customviewtest.util.DensityUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lsp on 2017/2/13.
 */

public class FlowLayout extends ViewGroup {

    List<ViewBean> list = new ArrayList<>();

    private IOnTabClickListener iOnTabClickListener;


    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setOnTabClickListener(IOnTabClickListener iOnTabClickListener) {
        this.iOnTabClickListener = iOnTabClickListener;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    public void initData(List<String> list) {
        ViewGroup.MarginLayoutParams pa = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            TextView tv = new TextView(getContext());
            tv.setText(list.get(i));
            tv.setTag(list.get(i));
            setOnclick(i,tv);
            addView(tv, pa);
            ((ViewGroup.MarginLayoutParams) tv.getLayoutParams()).setMargins(DensityUtils.dp2px(getContext(), 5), DensityUtils.dp2px(getContext(), 5),
                    DensityUtils.dp2px(getContext(), 5), DensityUtils.dp2px(getContext(), 5));
            tv.setBackgroundResource(R.drawable.flag_01);
        }
    }

    public void initData(List<String> list, int margin, int drawable) {
        ViewGroup.MarginLayoutParams pa = new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                ViewGroup.MarginLayoutParams.WRAP_CONTENT);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            TextView tv = new TextView(getContext());
            tv.setText(list.get(i));
            tv.setTag(list.get(i));
            setOnclick(i,tv);
            addView(tv, pa);
            ((ViewGroup.MarginLayoutParams) tv.getLayoutParams()).setMargins(dp2px(getContext(), margin), dp2px(getContext(), margin),
                    dp2px(getContext(), margin), dp2px(getContext(), margin));
            tv.setBackgroundResource(drawable);
        }
    }

    private void setOnclick(final int position, final TextView textView) {

        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                iOnTabClickListener.onTabClick(position, textView);
            }
        });
    }


    public int dp2px(Context context, float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //测量子view的大小
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //最终的宽和高
        int width = 0;
        int height = 0;
        //记录每一行的宽和高
        int lineWidth = 0;
        //子view数量
        int cCount = getChildCount();
        View cView;
        MarginLayoutParams params;
        int cWidth, cHeight;

        for (int i = 0; i < cCount; i++) {
            cView = getChildAt(i);
            params = (MarginLayoutParams) cView.getLayoutParams();
            cWidth = cView.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            cHeight = cView.getMeasuredHeight() + params.topMargin + params.bottomMargin;

            ViewBean viewBean = new ViewBean();

            if (lineWidth + cWidth < widthSize) {

                lineWidth += cWidth;

                height = Math.max(height, cHeight);

                width = Math.max(lineWidth, width);

            } else {

                width = Math.max(lineWidth, width);

                height += cHeight;

                lineWidth = cWidth;

            }

            viewBean.setLeft(lineWidth - cWidth + params.leftMargin);
            viewBean.setTop(height - cHeight + params.topMargin);
            viewBean.setRight(lineWidth - params.rightMargin);
            viewBean.setBottom(height - params.bottomMargin);

            list.add(viewBean);

        }
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width, heightMode == MeasureSpec.EXACTLY ? heightSize : height);
    }

    public interface IOnTabClickListener {

        void onTabClick(int position, TextView textView);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cCount = getChildCount();
        for (int i = 0; i < cCount; i++) {
            getChildAt(i).layout(list.get(i).getLeft(), list.get(i).getTop(), list.get(i).getRight(), list.get(i).getBottom());
        }

    }

    private class ViewBean {
        private int left;
        private int right;
        private int top;
        private int bottom;

        public int getLeft() {
            return left;
        }

        public void setLeft(int left) {
            this.left = left;
        }

        public int getRight() {
            return right;
        }

        public void setRight(int right) {
            this.right = right;
        }

        public int getTop() {
            return top;
        }

        public void setTop(int top) {
            this.top = top;
        }

        public int getBottom() {
            return bottom;
        }

        public void setBottom(int bottom) {
            this.bottom = bottom;
        }
    }


//    public FlowLayout(Context context) {
//        this(context, null);
//    }
//
//    public FlowLayout(Context context, AttributeSet attrs) {
//        this(context, attrs, 0);
//    }
//
//    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//
//    @Override
//    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
//    {
//        return new MarginLayoutParams(getContext(), attrs);
//    }
//
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //确定自己和子view的宽和高
//        measureChildren(widthMeasureSpec,heightMeasureSpec);
//
//        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//
//        int lineWidth = 0;
//        int lineHeight = 0;
//        int width = 0;
//        int height = 0;
//
//        int cCount = getChildCount();
//        for (int i = 0; i < cCount; i++) {
//            View cView = getChildAt(i);
//            MarginLayoutParams params = (MarginLayoutParams) cView.getLayoutParams();
//            int cWidth = cView.getMeasuredWidth()+params.leftMargin+params.rightMargin;
//            int cHeight = cView.getMeasuredHeight()+params.topMargin+params.bottomMargin;
//
//
//            if(lineWidth+cWidth>widthSize){
//                width = Math.max(lineWidth,cWidth);
//                lineWidth=cWidth;
//                lineHeight+=cHeight;
//                height+=lineHeight;
//                lineHeight=0;
//
//            }else {
//                lineWidth+=cWidth;
//                lineHeight = Math.max(lineHeight,cHeight);
//            }
//
//        }
//
//        setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? widthSize
//                : width, (heightMode == MeasureSpec.EXACTLY) ? heightSize
//                : height);
//    }
//    /**
//     * 存储所有的View，按行记录
//     */
//    private List<List<View>> mAllViews = new ArrayList<List<View>>();
//    private List<Integer> mLineHeight = new ArrayList<Integer>();
//    /**
//     * 记录每一行的最大高度
//     */
//    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        mAllViews.clear();
//        mLineHeight.clear();
//
//        int width = getWidth();
//
//        int lineWidth = 0;
//        int lineHeight = 0;
//        // 存储每一行所有的childView
//        List<View> lineViews = new ArrayList<View>();
//        int cCount = getChildCount();
//        // 遍历所有的孩子
//        for (int i = 0; i < cCount; i++)
//        {
//            View child = getChildAt(i);
//            MarginLayoutParams lp = (MarginLayoutParams) child
//                    .getLayoutParams();
//            int childWidth = child.getMeasuredWidth();
//            int childHeight = child.getMeasuredHeight();
//
//            // 如果已经需要换行
//            if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width)
//            {
//                // 记录这一行所有的View以及最大高度
//                mLineHeight.add(lineHeight);
//                // 将当前行的childView保存，然后开启新的ArrayList保存下一行的childView
//                mAllViews.add(lineViews);
//                lineWidth = 0;// 重置行宽
//                lineViews = new ArrayList<View>();
//            }
//            /**
//             * 如果不需要换行，则累加
//             */
//            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
//            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
//                    + lp.bottomMargin);
//            lineViews.add(child);
//        }
//        // 记录最后一行
//        mLineHeight.add(lineHeight);
//        mAllViews.add(lineViews);
//
//        int left = 0;
//        int top = 0;
//        // 得到总行数
//        int lineNums = mAllViews.size();
//        for (int i = 0; i < lineNums; i++)
//        {
//            // 每一行的所有的views
//            lineViews = mAllViews.get(i);
//            // 当前行的最大高度
//            lineHeight = mLineHeight.get(i);
//
//
//            // 遍历当前行所有的View
//            for (int j = 0; j < lineViews.size(); j++)
//            {
//                View child = lineViews.get(j);
//                if (child.getVisibility() == View.GONE)
//                {
//                    continue;
//                }
//                MarginLayoutParams lp = (MarginLayoutParams) child
//                        .getLayoutParams();
//
//                //计算childView的left,top,right,test2
//                int lc = left + lp.leftMargin;
//                int tc = top + lp.topMargin;
//                int rc =lc + child.getMeasuredWidth();
//                int bc = tc + child.getMeasuredHeight();
//
//
//                child.layout(lc, tc, rc, bc);
//
//                left += child.getMeasuredWidth() + lp.rightMargin
//                        + lp.leftMargin;
//            }
//            left = 0;
//            top += lineHeight;
//        }
//    }
}
